package pl.pelcra.cesar.spider.webparser.sites;

import org.apache.log4j.Logger;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import pl.pelcra.cesar.dateparser.datetime.DateTime;
import pl.pelcra.cesar.spider.webcrawler.CSWebURL;
import pl.pelcra.cesar.spider.webparser.interfaces.BasePageParser;
import pl.pelcra.cesar.spider.webparser.interfaces.IPageParserHelper;

public class EsoPageParser extends BasePageParser {

	private final Logger logger = Logger.getLogger(EsoPageParser.class);

	public EsoPageParser() {

	}

	public EsoPageParser(String parameters, int politenessDelay) {
		super(parameters, politenessDelay);
	}

	@Override
	public void init(CSWebURL webURL, String language, String doc_id) {
		super.init(webURL, "div[id=col3_content]");

		if (super.isGetPageSuccess()) {
			super.setPageParserHelper(new IPageParserHelper() {

				@Override
				public boolean filterContent(Element element) {
					Elements el = element.select("p[class=date]");
					if (el.size() == 0) {
						return true;
					}
					return false;
				}
			});
			super.setContentTag("p");

			this.setDoc_id(doc_id);

			super.setCategory(super.getMapParams().get("Category"));
			super.setLanguage(language);
			super.setSource_id(super.getMapParams().get("SourceId"));

			super.setPage_dateTag("p[class=date]");
			super.setParsed_pagedateFormat("dd MMM yyyy");

			this.setLanguage(language);
			super.setTitleTag("h1[class=pr_title]");
			super.setAuthorTag("");

			super.setNotes("");
			this.setUrlAttachment("");
		}
	}

	@Override
	public void setDoc_id(String doc_idTag) {
		doc_idTag = doc_idTag.substring(doc_idTag.length() - 8,
				doc_idTag.length() - 1);
		super.setDoc_id(doc_idTag);
	}

	@Override
	public void setLanguage(String language) {

		if (language.equalsIgnoreCase("en")) {
			language = "en";
		} else if (language.equalsIgnoreCase("germany")
				|| language.equalsIgnoreCase("austria")
				|| language.equalsIgnoreCase("belgium-de")
				|| language.equalsIgnoreCase("switzerland-de")) {
			language = "de";
		} else if (language.equalsIgnoreCase("belgium-nl")
				|| language.equalsIgnoreCase("netherlands")) {
			language = "nl";
		} else if (language.equalsIgnoreCase("belgium-fr")
				|| language.equalsIgnoreCase("france")
				|| language.equalsIgnoreCase("switzerland-fr")) {
			language = "fr";
		} else if (language.equalsIgnoreCase("brazil")
				|| language.equalsIgnoreCase("portugal")) {
			language = "pt";
		} else if (language.equalsIgnoreCase("chile")
				|| language.equalsIgnoreCase("spain")) {
			language = "es";
		} else if (language.equalsIgnoreCase("cyprus-tr")
				|| language.equalsIgnoreCase("turkey")) {
			language = "tr";
		} else if (language.equalsIgnoreCase("italy")
				|| language.equalsIgnoreCase("switzerland-it")) {
			language = "it";
		} else if (language.equalsIgnoreCase("czechrepublic")) {
			language = "cz";
		} else if (language.equalsIgnoreCase("denmark")) {
			language = "dk";
		} else if (language.equalsIgnoreCase("finland")) {
			language = "fi";
		} else if (language.equalsIgnoreCase("iceland")) {
			language = "is";
		} else if (language.equalsIgnoreCase("norway")) {
			language = "no";
		} else if (language.equalsIgnoreCase("poland")) {
			language = "pl";
		} else if (language.equalsIgnoreCase("russia")) {
			language = "ru";
		} else if (language.equalsIgnoreCase("sweden")) {
			language = "se";
		} else if (language.equalsIgnoreCase("ukraine")) {
			language = "uk";
		} else {
			this.logger.info(String.format(
					"Not defined languague tranformation for value=%s",
					language));
		}

		super.setLanguage(language);
	}

	@Override
	public void setParsed_pagedate() {
		DateTime myDateTime = new DateTime(this.getPage_date(),
				this.getLanguage(), this.getParsed_pagedateFormat());
		setParsed_pagedate(myDateTime.getParsed_pagedate());
	}
}